package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.contract.bean.BuyBean;

import java.util.ArrayList;

import contract.wwwarehouse.com.contract.R;


public class BuyAdapter extends BaseAdapter {

    private ArrayList<BuyBean.ListBean> mData;
    private Context mContext;
    private ViewHolder mHolder;
    private ListView mListView;

    public BuyAdapter(ArrayList<BuyBean.ListBean> data, Context context, ListView listView) {
        this.mData = data;
        this.mContext = context;
        mListView = listView;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.buy_item_listview, parent, false);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        final View finalConvertView = convertView;
        mListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ViewGroup.LayoutParams params = finalConvertView.getLayoutParams();
                params.height = (int) ((mListView.getHeight() - 10) / 5);
                finalConvertView.setLayoutParams(params);
                mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        ImageloaderUtils.displayImg(ImageloaderUtils.getImageLoader(), mData.get(position).getLogoUrl(), mHolder.mBuyLogo);
        mHolder.mBuyName.setText(mData.get(position).getBusinessName());
        if ("100".equals(mData.get(position).getTransactionStatus())) {
            mHolder.mBuyPayType.setVisibility(View.VISIBLE);
            mHolder.mBuyPayType.setText("周期付");
        }
        if ("10".equals(mData.get(position).getOrderStatus())) {
            mHolder.mBuyState.setText("待付款");
        } else if ("440".equals(mData.get(position).getOrderStatus())) {
            mHolder.mBuyState.setText("待发货");
        } else if ("450".equals(mData.get(position).getOrderStatus())) {
            mHolder.mBuyState.setText("待收货");
        } else if ("500".equals(mData.get(position).getOrderStatus())) {
            mHolder.mBuyState.setText("交易完成");
            mHolder.mBuyState.setTextColor(mContext.getResources().getColor(R.color.common_color_c13_23c0b1));
        } else if ("900".equals(mData.get(position).getOrderStatus())) {
            mHolder.mBuyState.setText("交易关闭");
            mHolder.mBuyState.setTextColor(mContext.getResources().getColor(R.color.common_color_c7_96999e));
        }
        mHolder.mBuyPrice.setText("¥" + mData.get(position).getTotalAmount());
        mHolder.mBuyOrderNumber.setText("订单号: " + mData.get(position).getContractId());
        mHolder.mBuyOrderTime.setText("下单时间: " + mData.get(position).getContractTime());
        return convertView;
    }

    class ViewHolder {
        private ImageView mBuyLogo;
        private TextView mBuyName;
        private TextView mBuyPayType;
        private TextView mBuyState;
        private TextView mBuyPrice;
        private TextView mBuyOrderNumber;
        private TextView mBuyOrderTime;

        public ViewHolder(View view) {
            mBuyLogo = (ImageView) view.findViewById(R.id.img_buy_logo);
            mBuyName = (TextView) view.findViewById(R.id.tv_buy_name);
            mBuyPayType = (TextView) view.findViewById(R.id.tv_buy_paytype);
            mBuyState = (TextView) view.findViewById(R.id.tv_buy_state);
            mBuyPrice = (TextView) view.findViewById(R.id.tv_buy_price);
            mBuyOrderNumber = (TextView) view.findViewById(R.id.tv_buy_ordernumber);
            mBuyOrderTime = (TextView) view.findViewById(R.id.tv_buy_ordertime);
        }
    }
}
